package com.gitee.gateway.core.db;

import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.statement.StatementContext;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 数据库模型实体类
 */
public abstract class Modal<T extends Modal> extends ModalRowMapper<T> implements Argument, Serializable {

    protected long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    /**
     * 该方法为 Argument 接口的实现，用于实现关联对象自动转 id 的功能
     *
     * @param position  参数位置
     * @param statement PreparedStatement 对象
     * @param ctx       StatementContext 对象
     * @throws SQLException SQL 异常
     */
    @Override
    public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
        statement.setLong(position, getId());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj instanceof Modal && obj.getClass().equals(this.getClass()))
            return ((Modal<?>) obj).getId() == this.getId();
        return false;
    }
}
